using System;
using System.Collections.Generic;
using System.Text;
using Ent.Web.Helper;
using System.Collections;
using System.Data;

namespace Ent.Web.Screener
{
    public class Sequence
    {
        XMLHelper xml = null;
        public Sequence(string xmlPath)
        {
            xml = new XMLHelper(xmlPath);
        }
        public Screen[] GetScreens(string featureName)
        {
            
            ArrayList list = new ArrayList();
            Screen[] sList = null;
            try
            {

                DataTable dt = xml.GetChildNodesAsDataTable("//Sequence/Feature[@name='" + featureName + "']", "Screen", new string[] { "feature", "id", "name", "path", "onsubmit", "onload", "disable" });
                ArrayList listId = new ArrayList();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Screen s = new Screen();
                    string feature = dt.Rows[i]["feature"] != null ? dt.Rows[i]["feature"].ToString() : "";
                    if (feature.Trim() != "")
                    {
                        Screen[] scrns = GetScreens(feature);
                        foreach (Screen scrn in scrns)
                            list.Add(scrn);
                        continue;
                    }
                    s.Id = dt.Rows[i]["id"] != null ? dt.Rows[i]["id"].ToString() : "";
                    if (listId.Contains(s.Id))
                        throw new Exception("XML Exception: Multiple screens with same id found.");
                    else
                        listId.Add(s.Id);
                    s.Name = dt.Rows[i]["name"] != null ? dt.Rows[i]["name"].ToString() : "";
                    s.Path = dt.Rows[i]["path"] != null ? dt.Rows[i]["path"].ToString() : "";
                    if (s.Path == "")
                    {
                        string cXPath = "//Sequence/Feature[@name='" + featureName + "']/Screen[@id='" + s.Id + "']/Custom";
                        s.CustomPath = xml.GetAttributeForSingleNode(cXPath, "path");
                        s.FormName = xml.GetAttributeForSingleNode(cXPath, "fromname");
                    }
                    s.OnSubmit = dt.Rows[i]["onsubmit"] != null ? dt.Rows[i]["onsubmit"].ToString() : "";
                    s.OnLoad = dt.Rows[i]["onload"] != null ? dt.Rows[i]["onload"].ToString() : "";
                    string pXPath = "//Sequence/Feature[@name='" + featureName + "']/Screen[@id='" + s.Id + "']";
                    s.Parameters = xml.GetNameValuePair(pXPath, "param", "name", "value");

                    bool disable = false;
                    bool.TryParse(dt.Rows[i]["disable"].ToString(), out disable);
                    //s.Disabled = disable; // dt.Rows[i]["disable"] != null ? bool.Parse(dt.Rows[i]["disable"].ToString()) : false;
                    if (!disable)
                        list.Add(s);
                }
                sList = new Screen[list.Count];
                for (int j = 0; j < list.Count; j++)
                {
                    sList.SetValue(list[j], j);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            return sList;
        }
    }
}
